import { Component } from '@angular/core';
import {Http,Response} from '@angular/http';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import 'rxjs/add/operator/map';
import { Rows } from '../../models/Rows';
import { Cols } from '../../models/Cols';
/**
 * Generated class for the FormShowPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-form-show',
  templateUrl: 'form-show.html',
})
export class FormShowPage {
  rows:Array<Rows>;
  errorMessage:any;

  title:string;
  tableName:string;
  tableSpaceName:string;
  comment:string='表单说明';

  constructor(
    public navCtrl: NavController, 
    public navParams: NavParams,
    public http:Http) {
  }

  ionViewDidLoad() {
    console.log('form');
    let table = this.navParams.get("table");
    if(table){
      console.log(JSON.stringify(table));
      this.title = table.title;
      this.tableName = table.tableName;
      this.tableSpaceName = table.tableSpaceName;
      this.comment = table.comment;
      this.rows = table.rows;
    }else{
      let id= this.navParams.get("id");
      let url = "";
      this.loadJsonData('assets/table.json').subscribe(
        result=>{
          this.title = result.title;
          this.tableName = result.tableName;
          this.tableSpaceName = result.tableSpaceName;
          this.comment = result.comment;
          this.rows = result.rows;
        },
        error=>{
          this.errorMessage = <any>error;
        }
      );
    }
    
  }

  loadJsonData(url : string) {
	  return this.http.get(url).map(
      (response: Response)=>response.json()
    );
  }

  getCheckBox(checked,id,field){
    console.log(checked);
console.log(id);
console.log(field.value);
    if(checked){
      field.value.push(id);
    }else{
      let index = field.value.indexOf(id);
      console.log("index:"+index);
      if(index>=0)
      field.value.splice(index,1);
    }
    console.log(field.value);
  }

  onSubmit(){
    console.log(this.rows);
  }

}
